import { defineStore } from 'pinia'


import { reqGetCategory1Data, reqGetCategory2Data, reqGetgory3Data } from '@/api/product/categorySelector/categorySelector'

//引入类型约束
import type { CategoryListStateDataModel } from "@/api/product/model/CategoryListModel"

export const useCategorySelectorStore = defineStore('CategorySelector', {
  state: (): CategoryListStateDataModel =>  {
    return { 

      category1List: [],
      category2List: [],
      category3List: [],
      category1Id: undefined,
      category2Id: undefined,
      category3Id:undefined

     }
  },
 
  actions: {
   //获取一级分类的数据
  async  getCategory1ListData() {
      const res = await reqGetCategory1Data()
      this.category1List = res;
    }
    ,
    //获取二级分类的数据
    async getCategory2ListData(category1Id: number) {
      //一级的数据的显示
      this.category1Id = category1Id
      //获取二级数据
      const res = await reqGetCategory2Data(category1Id)
      this.category2List = res;
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.category3List=[]
    },


    //获取三级分类的数据
    async getCategory3ListData(category2Id: number) {
      //二级数据显示
      this.category2Id=category2Id
      const res = await reqGetgory3Data(category2Id);
      this.category3List=res
      //二级数据以变化三级数据就变化了，input里的数据清空了
      this.category3Id = undefined
      
    },
    //存储三级分类id的方法
    SaveCategory3Id(category3Id: number) {
      //将三级id存入pina中，
      
      this.category3Id=category3Id
    }
  },
})